package org.thunms.framework.views;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thunms.framework.utils.UtilSystemProperties;

public class HeadTag extends TagSupport {
	

	private static final long serialVersionUID = 5676635377972582729L;
	private static final Logger logger = LoggerFactory.getLogger(HeadTag.class);
	private boolean portal;
	
	private boolean useJquery=true;
	private boolean useEasyUI=true;
	private boolean loadJqueryAndEasyUI = true;
	
	private boolean loadAuto=true;
	
	private boolean useThunmsui=true;
	
	private boolean useCBMPCSS=false;
	
	
	private boolean useAutoEnd=true;
	
	private boolean useBaseUtils=true;
	
	private boolean useBaseCss=true;

	private String autoLoadJavaScript;
	private String autoLoadCss;
	
	private boolean useXheditor=true;
	
	private Boolean useMy97DatePicker=false;
	
	
	private Boolean useKindeditor=false;
	
	
	private Boolean useLodop=false;
	
	private Boolean useUeditor=false;
	
	private Boolean useEdatagrid=false;
	
	
	private Boolean baseSupport=false;

	private String target=null;
	
	private String description;
	
	//@Cacheable(value = "thunmsTagsCache")
	@Override
	public int doStartTag() throws JspException {
		 String jqueryVersion = UtilSystemProperties.getPropertyValue("thunms.platform.jqueryVersion", "180");
		 String easyuiVersion = UtilSystemProperties.getPropertyValue("thunms.platform.easyuiVersion", "132");
		try {
			String cxt = this.pageContext.getServletContext().getContextPath();
			this.pageContext.setAttribute("cxt", cxt);
			JspWriter out = this.pageContext.getOut();
			
			StringBuffer head = new StringBuffer();

			head.append("<head>");
			if(StringUtils.isNotBlank(this.getTarget())){
				head.append("<base  target=\"").append(this.getTarget()).append("\" >");
			}
			
			head.append("<title>").append(UtilSystemProperties.getPropertyValue("thunms.platform.title", "任务调度管理平台")).append("</title>");
			//head.append("<link rel=\"shortcut icon\" href=\"").append(cxt).append("/images/icon/favicon.ico\">");
			head.append("<meta http-equiv=\"X-UA-Compatible\"content=\"IE=9;IE=8;IE=7;IE=EDGE\">");
			head.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
			head.append("<meta http-equiv=\"pragma\" content=\"no-cache\">");
			head.append("<meta http-equiv=\"cache-control\" content=\"no-cache\">");
			head.append("<meta http-equiv=\"expires\" content=\"0\">");
			head.append("<meta http-equiv=\"keywords\" content=\"").append(UtilSystemProperties.getPropertyValue("thunms.platform.keywords","任务调度管理平台")).append("\">");
			head.append("<meta http-equiv=\"description\" content=\"").append(UtilSystemProperties.getPropertyValue("thunms.platform.description", "一站式任务调度解决方案  "));
			if(StringUtils.isNotBlank(this.getDescription())){
				head.append(this.getDescription());	
			}
			head.append(" \">");
			
			if(this.loadAuto){//是否智能加载
				if(this.useJquery){
					head.append("<script src=\"").append(cxt).append("/script/jquery/").append(jqueryVersion).append("/jquery.min.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
					head.append("<script src=\"").append(cxt).append("/script/jquery.cookie.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
				}
				
				
				
				if(!this.baseSupport&&this.useEasyUI){
					head.append("<link id=\"easyuiTheme\" cxt=\"").append(cxt).append("\"  href=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/themes/default/easyui.css\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");
				head.append("<script src=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/jquery.easyui.min.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
				
				//jquery.checkbox.js
				//head.append("<script src=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/jquery.checkbox.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
				//jquery.radio.js
				//head.append("<script src=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/jquery.radio.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
				//validatebox fixed版 解决validatebox初始化验证
				head.append("<script src=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/jquery.validatebox.fixed.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
				head.append("<script src=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/jquery.validatebox.expand.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
				//layout折叠后显示标题
				head.append("<script src=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/locale/easyui-lang-zh_CN.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
				}
				
				
				
				
				if(!this.baseSupport&&this.useEasyUI){
				head.append("<link href=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/themes/icon.css\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");
				}
				if (this.portal) {
					head.append("<script src=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/portal/jquery.portal.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
					head.append("<link href=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/portal/portal.css\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");
				}
				if (this.loadJqueryAndEasyUI&&this.useBaseUtils&&!this.baseSupport) {
					head.append("<script src=\"").append(cxt).append("/script/thunms_util.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
				}
				
				
				
				
				
				//加载My97DatePicker
				if(this.useMy97DatePicker){
					String My97DatePickerVersion=UtilSystemProperties.getPropertyValue("thunms.platform.My97DatePickerVersion", "48");
					head.append("<script src=\"").append(cxt).append("/script/My97DatePicker/").append(My97DatePickerVersion).append("/WdatePicker.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
					if(!this.baseSupport&&this.useEasyUI){
						head.append("<script src=\"").append(cxt).append("/script/jquery/easyui/").append(easyuiVersion).append("/easyui.my97DatePicker.js\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
					}
					
				}
				
				
				
				
				
				
				
	
				if (this.autoLoadJavaScript != null && !"".equals(this.autoLoadJavaScript.trim())) {
					String[] paths = this.autoLoadJavaScript.split(",");
					for (String path : paths) {
						if (StringUtils.isNotBlank(path)) {
							head.append("<script src=\"").append(cxt).append("/script").append(path).append("\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
						}
					}
				}
				//加载扩展的thunmsScriptHeadExpandLoad
				if (UtilSystemProperties.getPropertyValue("thunmsScriptHeadExpandLoad") != null) {
					String[] paths = UtilSystemProperties.getPropertyValue("thunmsScriptHeadExpandLoad").split(",");
					for (String path : paths) {
						if (StringUtils.isNotBlank(path)) {
							head.append("<script src=\"").append(cxt).append("/script").append(path).append("\" charset=\"UTF-8\" type=\"text/javascript\"></script>");
						}
					}
				}
				if(!this.baseSupport&&this.useBaseCss){
					head.append("<link href=\"").append(cxt).append("/css/thunms.css\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");	
				}
				
				if (this.autoLoadCss != null && !"".equals(this.autoLoadCss.trim())) {
					String[] paths = this.autoLoadCss.split(",");
					for (String path : paths) {
						if (StringUtils.isNotBlank(path)) {
							head.append("<link href=\"").append(cxt).append("/css").append(path).append("\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");
						}
					}
				}
				
				
				
				
				//加载扩展的thunmsCsstHeadExpandLoad
				if (UtilSystemProperties.getPropertyValue("thunmsCsstHeadExpandLoad") != null) {
					String[] paths = UtilSystemProperties.getPropertyValue("thunmsCsstHeadExpandLoad").split(",");
					for (String path : paths) {
						if (StringUtils.isNotBlank(path)) {
							head.append("<link href=\"").append(cxt).append("/css").append(path).append("\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");
						}
					}
				}
			
			}
			
			this.headTagCallBack(head, cxt);
			if(this.useAutoEnd){
				head.append("</head>");
			}
			
			//head.append("<body>");

			out.print(head);

		} catch (IOException e) {
			logger.error("head解析过程成出错", e);
		}
		return SKIP_BODY;
	}

	public boolean isPortal() {
		return portal;
	}

	public void setPortal(boolean portal) {
		this.portal = portal;
	}

	public boolean isLoadJqueryAndEasyUI() {
		return loadJqueryAndEasyUI;
	}

	public void setLoadJqueryAndEasyUI(boolean loadJqueryAndEasyUI) {
		this.loadJqueryAndEasyUI = loadJqueryAndEasyUI;
	}

	public String getAutoLoadJavaScript() {
		return autoLoadJavaScript;
	}

	public void setAutoLoadJavaScript(String autoLoadJavaScript) {
		this.autoLoadJavaScript = autoLoadJavaScript;
	}

	public String getAutoLoadCss() {
		return autoLoadCss;
	}

	public void setAutoLoadCss(String autoLoadCss) {
		this.autoLoadCss = autoLoadCss;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	protected void headTagCallBack(StringBuffer head, String cxt) {

	}

	public void setUseMy97DatePicker(Boolean useMy97DatePicker) {
		this.useMy97DatePicker = useMy97DatePicker;
	}

	

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Boolean getUseKindeditor() {
		return useKindeditor;
	}

	public void setUseKindeditor(Boolean useKindeditor) {
		this.useKindeditor = useKindeditor;
	}

	

	public Boolean getUseLodop() {
		return useLodop;
	}

	public void setUseLodop(Boolean useLodop) {
		this.useLodop = useLodop;
	}

	

	public void setUseEdatagrid(Boolean useEdatagrid) {
		this.useEdatagrid = useEdatagrid;
	}

	

	public void setBaseSupport(Boolean baseSupport) {
		this.baseSupport = baseSupport;
	}

	public void setUseJquery(boolean useJquery) {
		this.useJquery = useJquery;
	}

	public void setUseBaseUtils(boolean useBaseUtils) {
		this.useBaseUtils = useBaseUtils;
	}

	public void setUseBaseCss(boolean useBaseCss) {
		this.useBaseCss = useBaseCss;
	}

	public void setUseEasyUI(boolean useEasyUI) {
		this.useEasyUI = useEasyUI;
	}

	public void setUseXheditor(boolean useXheditor) {
		this.useXheditor = useXheditor;
	}

	public void setUseAutoEnd(boolean useAutoEnd) {
		this.useAutoEnd = useAutoEnd;
	}

	

	public void setUseThunmsui(boolean useThunmsui) {
		this.useThunmsui = useThunmsui;
	}

	

	public void setUseCBMPCSS(boolean useCBMPCSS) {
		this.useCBMPCSS = useCBMPCSS;
	}

	public void setLoadAuto(boolean loadAuto) {
		this.loadAuto = loadAuto;
	}

	public void setUseUeditor(Boolean useUeditor) {
		this.useUeditor = useUeditor;
	}

	
}
